package in.muton.edushare.pages.user;

import java.util.Date;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.GroupsDAO;
import com.edushare.common.vo.Groups;

import in.muton.edushare.helper.TinyMCEHelper;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

public class CreateGroupForm extends EdusharePanel {
	private Groups group;
	private Panel nextPanel;
	public CreateGroupForm(String id) {
		super(id);
		add(new FormFrag("activeFrag", this));
		nextPanel = new EmptyPanel("nextFragDiv");
		add(nextPanel);
	}
	
	public Groups getGroup(){
		return this.group;
	}
	
	public class FormFrag extends Fragment{

		public FormFrag(String id, MarkupContainer markupProvider) {
			super(id, "formFrag", markupProvider);
			group = new Groups();
			Form<Void> form = new Form<Void>("groupForm"){

				private static final long serialVersionUID = 4599794342872193777L;

				@Override
				protected void onSubmit() {
					group.setCreator(getLoggedInUser());
					group.setCreationTimestamp(new Date());
					if(getLoggedInUser().getRoleList()==null || getLoggedInUser().getRoleList().size()<1){
						throw new RestartResponseException(URLLoader.getPageClass(URLConstants.UNAUTHORIZED_PATH));
					}
					group.setUniversity(getLoggedInUser().getRoleList().get(0).getUniversity());
					new GroupsDAO(getORMInterface()).createNewGroup(group);
					FormFrag.this.setVisible(false);
					nextPanel.replaceWith(new NextFrag("nextFragDiv", CreateGroupForm.this));
				}
			};
			form.setOutputMarkupId(true);
			form.setMarkupId("groupForm");
			add(form);
			
			TextField<String> name = new TextField<String>("gname", new PropertyModel<String>(CreateGroupForm.this, "group.groupName"));
			name.setOutputMarkupId(true);
			name.setMarkupId("gname");
			form.add(name);
			TextArea<String> desc = new TextArea<String>("gdesc", new PropertyModel<String>(CreateGroupForm.this, "group.description"));
			desc.add(TinyMCEHelper.getDefaultTinyMCEBehavior());
			desc.setOutputMarkupId(true);
			desc.setMarkupId("gdesc");
			form.add(desc);
		}
		
	}
	
	
	public class NextFrag extends Fragment{

		public NextFrag(String id,MarkupContainer markupProvider) {
			super(id, "nextFrag", markupProvider);
			add(new EduShareURLPanel("ngrouplink", new CayenneModel<Groups>(group)));
			add(new BookmarkablePageLink<Void>(
					"ggo", 
					EntityPageParams.getEntityHomePage(group), 
					EntityPageParams.getParams(new CayenneModel<Groups>(CreateGroupForm.this.group))
				));
			add(new BookmarkablePageLink<Void>("gback", URLLoader.getPageClass(URLConstants.USER_GROUPS)));
		}
		
	}
	
}
